﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using jabber.protocol;
using System.Xml;

namespace RoketPack.IM
{
    class CustomPacket : Packet
    {
        private string p_Type = null;
        private string p_Message = null;

        /// <summary>
        /// Initalizes a new CustomPacket with the specified XmlDocument.
        /// </summary>
        /// <param name="doc"></param>
        public CustomPacket(XmlDocument doc)
            : base("message", doc)
        {
            this.ID = Element.NextID();
        }
        
        /// <summary>
        /// Initalizes a new CustomPacket with the specified data.
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="qname"></param>
        /// <param name="doc"></param>
        public CustomPacket(string prefix, XmlQualifiedName qname, XmlDocument doc)
            : base(qname.Name, doc)  // Note:  *NOT* base(prefix, qname, doc), so that xpath matches are easier
        {
        }

        /// <summary>
        /// Gets a new CustomPacket instance based on the XmlNode (acts
        /// as a specialised upcast where the XmlNode is not yet of type
        /// CustomPacket).
        /// </summary>
        /// <param name="i">The XmlNode which is not of CustomPacket type, but holds equivilant data.</param>
        /// <returns>A CustomPacket instance that represents the XmlNode.</returns>
        public static CustomPacket GetCustomPacketFromNode(XmlNode i)
        {
            XmlDocument d = new XmlDocument();
            CustomPacket c = new CustomPacket(d);
            c.p_Type = i.Attributes["type"].Value;
            c.p_Message = i.InnerText;
            return c;
        }

        /// <summary>
        /// The text data associated with this custom packet.
        /// </summary>
        public string Message
        {
            get
            {
                if (this.p_Message != null)
                    return this.p_Message;
                
                if (this["rp"] == null)
                    this.AppendChild(this.CreateChildElement<RoketPackXmlElement>());
                if (this["rp"] is RoketPackXmlElement)
                    return (this["rp"] as RoketPackXmlElement).Message;
                else
                    return null;
            }
            set
            {
                if (this["rp"] == null)
                    this.AppendChild(this.CreateChildElement<RoketPackXmlElement>());
                if (this["rp"] is RoketPackXmlElement)
                    (this["rp"] as RoketPackXmlElement).Message = value;
                else
                    throw new XmlException("The message's <rp> element must be of type RoketPackXmlElement.");
            }
        }

        /// <summary>
        /// The custom RoketPack packet type.
        /// </summary>
        public string Type
        {
            get
            {
                if (this.p_Type != null)
                    return this.p_Type;

                if (this["rp"] == null)
                    this.AppendChild(this.CreateChildElement<RoketPackXmlElement>());
                if (this["rp"] is RoketPackXmlElement)
                    return (this["rp"] as RoketPackXmlElement).Type;
                else
                    return null;
            }
            set
            {
                if (this["rp"] == null)
                    this.AppendChild(this.CreateChildElement<RoketPackXmlElement>());
                if (this["rp"] is RoketPackXmlElement)
                   (this["rp"] as RoketPackXmlElement).Type = value;
                else
                    throw new XmlException("The message's <rp> element must be of type RoketPackXmlElement.");
            }
        }
    }
}
